Passed
Pull Request — develop (#758)
by Kevin Van
08:51 queued 04:39
created

MatchTeaser.tsx ➔ getData   C

Complexity

Conditions 10

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 5.9999
c 0
b 0
f 0
cc 10

How to fix   Complexity   

Complexity

Complex classes like MatchTeaser.tsx ➔ getData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import { Match } from "../Types/Match"
2
import { MatchTeaserDetailProps, MatchTeaserProps, MatchTeasersProps } from "../Types/MatchTeaser"
3
import { useSiteMetaData } from "../hooks/use-site-metadata"
4
import { mapPsdStatus, request } from "../scripts/helper"
5
import "./MatchTeaser.scss"
6
import classNames from "classnames"
7
import moment from "moment"
8
import "moment-timezone"
9
import "moment/locale/nl-be"
10
import React, { useEffect, useState } from "react"
11
import LazyLoad from "react-lazyload"
12
13
export const MatchTeaserDetail = ({ match, includeRankings }: MatchTeaserDetailProps) => {
14
  moment.tz.setDefault(`Europe/Brussels`)
15
  moment.locale(`nl-be`)
16
  moment.localeData(`nl-be`)
17
18
  const d = moment(match.date)
19
  const matchPlayed =
20
    ((match.status === 0 || match.status === null) && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) ||
21
    false
22
23
  return (
24
    <article className="match__teaser">
25
      <header>
26
        <h3>{match.teamName.replace(`Voetbal : `, ``)}</h3>
27
        {match.status !== 0 && (
28
          <div className="match__teaser__datetime__wrapper match__teaser__datetime__wrapper--status">
29
            <time
30
              className="match__teaser__datetime match__teaser__datetime--date"
31
              dateTime={d.format(`YYYY-MM-DD - H:mm`)}
32
            >
33
              {d.format(`dddd DD MMMM - H:mm`)}
34
            </time>
35
            <span className="match__teaser__datetime match__teaser__datetime--status">
36
              {mapPsdStatus(match.status)}
37
            </span>
38
          </div>
39
        )}
40
        {(match.status === 0 || match.status === null) && (
41
          <div className="match__teaser__datetime__wrapper">
42
            <time className="match__teaser__datetime match__teaser__datetime--date" dateTime={d.format(`YYYY-MM-DD`)}>
43
              {d.format(`dddd DD MMMM`)}
44
            </time>
45
            {` `}-{` `}
46
            <time className="match__teaser__datetime match__teaser__datetime--time" dateTime={d.format(`H:mm`)}>
47
              {d.format(`H:mm`)}
48
            </time>
49
          </div>
50
        )}
51
      </header>
52
      <main>
53
        <div
54
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
55
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
56
          })}
57
        >
58
          <LazyLoad debounce={false}>
59
            <img
60
              src={match.homeClub?.logo}
61
              alt={match.homeClub?.abbreviation}
62
              className="match__teaser__logo match__teaser__logo--home"
63
            />
64
          </LazyLoad>
65
          {match.homeClub?.abbreviation || match.homeClub?.name}
66
        </div>
67
68
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
69
        {matchPlayed && (
70
          <div className="match__teaser__vs match__teaser__vs--score">
71
            <div className="match__teaser__vs--score--home">{match.goalsHomeTeam}</div>-
72
            <div className="match__teaser__vs--score--away">{match.goalsAwayTeam}</div>
73
          </div>
74
        )}
75
76
        <div
77
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
78
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
79
          })}
80
        >
81
          <LazyLoad debounce={false}>
82
            <img
83
              src={match.awayClub?.logo}
84
              alt={match.awayClub?.abbreviation}
85
              className="match__teaser__logo match__teaser__logo--away"
86
            />
87
          </LazyLoad>
88
          {match.awayClub?.abbreviation || match.awayClub?.name}
89
        </div>
90
      </main>
91
      {/* {includeRankings && match.competitionType === `Competitie` && (
92
        <MiniRanking
93
          teamId={match.homeTeamId || match.awayTeamId}
94
          homeTeam={match.homeClub?.name}
95
          awayTeam={match.awayClub?.name}
96
        />
97
      )} */}
98
    </article>
99
  )
100
}
101
102
export const MatchTeaser = ({ teamId, action, includeRankings }: MatchTeaserProps) => {
103
  if (action !== `prev` && action !== `next`) {
104
    throw new Error(`Invalid action provided`)
105
  }
106
107
  const [data, setData] = useState<Match[]>([])
108
109
  const { kcvvPsdApi } = useSiteMetaData()
110
111
  useEffect(() => {
112
    async function getData() {
113
      const response = await request.get(`${kcvvPsdApi}/matches/${action}`, {
114
        params: { include: teamId },
115
      })
116
      setData(response.data)
117
    }
118
    getData()
119
  }, [action, kcvvPsdApi, teamId])
120
121
  if (data.length > 0) {
122
    return <MatchTeaserDetail match={data[0]} includeRankings={includeRankings} />
123
  } else {
124
    return <div className="match__teaser__no_match">Geen wedstrijd gevonden</div>
125
  }
126
}
127
128
export const MatchTeasers = ({ teamId, includeRankings = false }: MatchTeasersProps) => (
129
  <div className="match__teasers__wrapper full-width">
130
    <div className="match__teasers__inner">
131
      <div className="match__teasers match__teasers--prev">
132
        <header className="match__teasers__header">Vorige</header>
133
        <MatchTeaser teamId={teamId} action="prev" includeRankings={includeRankings} />
134
      </div>
135
      <div className="match__teasers match__teasers--next">
136
        <header className="match__teasers__header">Volgende</header>
137
        <MatchTeaser teamId={teamId} action="next" includeRankings={includeRankings} />
138
      </div>
139
    </div>
140
  </div>
141
)
142
143
MatchTeaser.defaultProps = { includeRankings: false }
144
MatchTeasers.defaultProps = { includeRankings: false }
145
MatchTeaserDetail.defaultProps = { includeRankings: false }
146